// pages/vote/vote.js
import { Vote } from '../../model/vote.js'
const vote = new Vote();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    changeIndex:'',
    indicatorDots:false,
    id:'',
    voteimgs:[],//轮播图
    votedetail:{},//详情对象
    detailList:[],//大列表
    optionVoList:[],//
    optionId:'',//IDstring
    questionId:'',//问题ID
    optionIds:[],//选项id数组
    classId:'',//活动id
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    this.setData({
      id:options.id
    })
    
    this.votedetail()
  },
  // navBar返回
  handlerGobackClick() {
    const pages = getCurrentPages();
    if (pages.length >= 2) {
      wx.navigateBack({
        delta: 1
      });
    } else {
      wx.navigateTo({
        url: '/pages/firstpage/firstpage'
      });
    }
  },
  // 选择
  checkboxChange: function (e) {
    console.log(e)
    console.log('checkbox发生change事件，携带value值为：', e.currentTarget.dataset.index);
    var index = e.currentTarget.dataset.index;
    console.log(index);
    this.setData({
      changeIndex:index
    })
    var optionVoList = this.data.optionVoList, values = e.currentTarget.dataset.index;
    var optionIds = this.data.optionIds
    
    optionVoList[values].checked = !optionVoList[values].checked
    if (optionVoList[values].checked){
        optionIds.push(optionVoList[values].id)
        console.log(this.data.optionIds)
      }
      // var optionId=optionIds.join(',')
     else{
      for (var j in optionIds){
        if (optionVoList[values].id == optionIds[j]){
            optionIds.splice(j,1)
         }
       }
    }

    this.setData({
      optionIds:optionIds
    });
    console.log(this.data.optionIds)
  },
  // 单选
  radioChange(e){
    var arr = []
    arr.push(e.detail.value)
    this.setData({
      optionIds:arr
    })
    for(var i in this.data.detailList){
      var option = this.data.detailList[i].optionVoList;
    }
    for(var i in option){
      if(option[i].id == e.detail.value){
          var index = i;
          this.setData({
            changeIndex:index
          })
          return false;
      }
    }


   
  
  },
  //详情
  votedetail(){
    var obj={id:this.data.id}
    vote.vote_detail(obj).then(res=>{
      console.log(res)
      var optionVoList=this.data.optionVoList
      var voteimgs=this.data.voteimgs
      // var questionId=this.data.questionId
      for(var k=0;k<res.data.picList.length;k++){
        voteimgs.push(res.data.picList[k])
      }
      for(var g=0;g<res.data.questionInfoList.length;g++){
        this.setData({
          classId:res.data.questionInfoList[0].classId,
          questionId:res.data.questionInfoList[g].id
        })
        res.data.questionInfoList[g].isChoses = 0
        console.log(this.data.questionId)
        for(var i in res.data.questionInfoList[g].optionVoList){
          res.data.questionInfoList[g].optionVoList[i].checked=false
          if(res.data.questionInfoList[g].optionVoList[i].isChose ==1){
            res.data.questionInfoList[g].isChoses = 1
          }
          optionVoList.push(res.data.questionInfoList[g].optionVoList[i])
        }
      }
      if(res.data.isBegin==1){
        wx.showToast({
          title: '投票活动未开始',
          icon:'none'
        })
      }else if(res.data.isBegin==2 && res.data.isJoin==1){
        wx.showToast({
          title: '您已投票',
          icon:'none'
        })
      }else if(res.data.isBegin==3 && res.data.isJoin==1){
        wx.showToast({
          title: '您已投票',
          icon:'none'
        })
      }else if(res.data.isBegin==3 && res.data.isJoin==0){
        wx.showToast({
          title: '活动已结束',
          icon:'none'
        })
      }
      this.setData({
        votedetail:res.data,
        detailList:res.data.questionInfoList,//大
        voteimgs:voteimgs,
        optionVoList:optionVoList,//小
        // classId:res.data.questionInfoList[0].classId
        // questionId:questionId
        isJoin:res.data.isJoin
      })
      
      console.log(this.data.questionId)
    }).catch(error=>{
      console.log(error)
    })
  },
  //投票按钮
  bindtap(e){
    console.log(e)
    // console.log(this.data.questionId)
    this.change()
  },
  //投票
  change(){
    var that=this
    var liat = that.data.optionIds.join(',')
    var obj={
      id:that.data.classId,
      questionInfoId:that.data.questionId,
      optionIds: liat
    }
    if(obj.questionInfoId==''){
      wx.showToast({
        title: '请先做出选择',
        icon:'none',
        duration:2000
      })
    }else{
      wx.showModal({
        title: '提示',
        content: '您确定要投票吗',
        success (res) {
          if (res.confirm) {
            console.log('用户点击确定')
            vote.change_vote(obj).then(res=>{
              console.log(res)
              for( var i in that.data.optionIds){
                for( var t in that.data.optionVoList){
                  if(that.data.optionIds[i] == that.data.optionVoList[t].id){
                    that.data.detailList[0].optionVoList[t].isChose = 1;
                  }
                }
              }
              // that.data.detailList[0].optionVoList[that.data.changeIndex].isChose = 1;
                that.setData({
                  detailList:that.data.detailList
                })
              if(res.code==0 && res.data.notice!=''){
                
                wx.showModal({
                  title: '提示',
                  content: res.data.notice,
                  showCancel:false,
                  success (res) {
                    if (res.confirm) {
                    console.log('用户点击确定')
                      wx.navigateBack({
                        complete: (res) => {},
                        delta:1
                      })
                    }
                  }
                })
  
                // wx.showToast({
                //   title: res.data.notice,
                //   icon:'none',
                //   duration:3000
                // })
  
                var votedetail=that.data.votedetail
                var detailList=that.data.detailList
                  detailList[0].isChoses = 1
                  votedetail.isJoin=1
                that.setData({
                  
                  votedetail:votedetail,
                  detailList:detailList
                })
              }else if(res.code==1){
                wx.showToast({
                  title: res.msg,
                  icon:'none',
                  duration:3000
                })
                return false
              }
  
              
            }).catch(error=>{
              console.log(error)
            })
  
            // wx.showToast({
            //   title: '投票成功',
      
            // })
            
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
    }
    
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})